package com.regedit.aplicacion.adapters;

import android.content.Context;
import android.database.Cursor;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

import com.regedit.aplicacion.R;
import com.regedit.aplicacion.dao.EntityDao;
import com.regedit.aplicacion.models.Carro;

public class MiCursorAdapter extends SimpleCursorAdapter {

	private Context mContext;

	@SuppressWarnings("deprecation")
	public MiCursorAdapter(final Context context, final int layout,
			final Cursor cursor, final String[] arrFrom, final int[] arrTo) {
		super(context, layout, cursor, arrFrom, arrTo);
		mContext = context;
	}

	/**
	 * Obtiene el numero total de items a mostrar
	 */
	@Override
	public int getCount() {
		return getCursor().getCount();
	}

	@Override
	public Object getItem(int position) {

		final Cursor cursor = getCursor();
		cursor.moveToPosition(position);
		final Carro carro = new Carro();
		carro.setMarca(cursor.getString(cursor
				.getColumnIndexOrThrow(EntityDao.COL_TITLE)));
		return carro;
	}

	/**
	 * Obtiene el view del nuevo item para mostrarlo en pantalla
	 */
	@Override
	public View getView(final int position, View convertView,
			final ViewGroup parent) {

		Carro carro = (Carro) getItem(position);
		final LinearLayout viewRelative = (LinearLayout) super.getView(
				position, convertView, parent);

		TextView txtMarca = (TextView) viewRelative.findViewById(R.id.marca);
		txtMarca.setText(carro.getMarca());

		return viewRelative;
	}

	/**
	 * Set all items disabled
	 */
	@Override
	public boolean areAllItemsEnabled() {
		return false;
	}

	/**
	 * Metodo usado cuando el adaptador es notificado que la data en el cursor
	 * ha cambiado
	 */
	@Override
	protected void onContentChanged() {
		super.onContentChanged();
		notifyDataSetChanged();
	}

}